﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using TracyGUI.View;
using SharedLib.WinFormMVCFramework;
using System.Threading;
using SharedLib.MessageFramework.Messages;

namespace TracyGUI.Controller
{
    class StatisticsViewController : ControllerBase<IStatisticsView>, IController<IStatisticsView>
    {
        protected override IStatisticsView ResolveView()
        {
            return Tracy.TracyFacade.Instance.DIContainer.Resolve<IStatisticsView>();
        }

        public StatisticsViewController()
        {
            Tracy.TracyFacade.Instance.MessageDispatcher.MessageProcessing += new EventHandler<SharedLib.GenericEventArgs<IAppMessage>>(MessageDispatcher_MessageProcessing);
            Tracy.TracyFacade.Instance.MessageDispatcher.MessageProcessed += new EventHandler<SharedLib.GenericEventArgs<IAppMessage>>(MessageDispatcher_MessageProcessed);
        }

        private DateTime _lastRefreshed = DateTime.Now;
        private int _processingMessages = 0;
        void MessageDispatcher_MessageProcessing(object sender, SharedLib.GenericEventArgs<IAppMessage> e)
        {
            int currentProcessingMessages = Interlocked.Increment(ref _processingMessages);
            DisplayStatusTextTwiceASecond(currentProcessingMessages);
        }

        private int _lateUpdateScheduled = 0;
        private void DisplayStatusTextTwiceASecond(int messageCount)
        {
            DateTime now = System.DateTime.Now;
            if ((now - _lastRefreshed).TotalMilliseconds > 500)
            {
                _lastRefreshed = now;
                DisplayStatusText(messageCount);
            }
            else
            {
                if (Interlocked.Exchange(ref _lateUpdateScheduled, 1) == 0) //first enter
                {
                    Thread lateUpdate = new Thread(() =>
                    {
                        do
                        {
                            Thread.Sleep(5000);
                        } while ((System.DateTime.Now - _lastRefreshed).TotalMilliseconds <= 500);//Make sure the last update happened older than 0.5 seconds ago
                        DisplayStatusText(_processingMessages);
                        Interlocked.Exchange(ref _lateUpdateScheduled, 0); //clear flag after call
                    });
                    lateUpdate.Start();
                }
            }
        }

        private void DisplayStatusText(int messageCount)
        {
            if (messageCount <= 0)
                View.DisplayStatusText(Tracy.TracyFacade.Instance.LanguageManager.GetText("StatusReady"));
            else if (messageCount == 1)
                View.DisplayStatusText(Tracy.TracyFacade.Instance.LanguageManager.GetText("ProcessingOneTask"));
            else
                View.DisplayStatusText(String.Format(Tracy.TracyFacade.Instance.LanguageManager.GetText("ProcessingMultiTasks"), messageCount));
        }
        void MessageDispatcher_MessageProcessed(object sender, SharedLib.GenericEventArgs<IAppMessage> e)
        {
            int currentProcessingMessages = Interlocked.Decrement(ref _processingMessages);
            DisplayStatusTextTwiceASecond(currentProcessingMessages);
        }
    }
}
